from PIL import Image, ImageDraw, ImageFont
import random

# 定义HEX色彩转RGB色彩的函数
def hex_to_rgb(hex_color):
    hex_color = hex_color.lstrip('#')
    return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))


# 定义渐变色彩范围数组
color_ranges = [
    [(107,203,165), (202,244,194)],  # 绿
    [hex_to_rgb('#EECE13'), hex_to_rgb('#B210FF')],
    [hex_to_rgb('#79F1A4'), hex_to_rgb('#0E5CAD')],
    [hex_to_rgb('#FDD819'), hex_to_rgb('#E80505')],

    [hex_to_rgb('#81FBB8'), hex_to_rgb('#28C76F')],
    [hex_to_rgb('#E280FF'), hex_to_rgb('#9F44D3')],
    [hex_to_rgb('#F97794'), hex_to_rgb('#623AA2')],

    [hex_to_rgb('#FCCF31'), hex_to_rgb('#F55555')],
    [hex_to_rgb('#F761A1'), hex_to_rgb('#8C1BAB')],
    [hex_to_rgb('#43CBFF'), hex_to_rgb('#9708CC')],

    [hex_to_rgb('#FFF3B0'), hex_to_rgb('#CA26FF')],
    [hex_to_rgb('#FFF5C3'), hex_to_rgb('#9452A5')],
    [hex_to_rgb('#F05F57'), hex_to_rgb('#360940')],

    [hex_to_rgb('#52E5E7'), hex_to_rgb('#130CB7')],
    [hex_to_rgb('#F1CA74'), hex_to_rgb('#A64DB6')],
    [hex_to_rgb('#E8D07A'), hex_to_rgb('#5312D6')],

    [hex_to_rgb('#92FFC0'), hex_to_rgb('#002661')],
    [hex_to_rgb('#EEAD92'), hex_to_rgb('#6018DC')],
    [hex_to_rgb('#F6CEEC'), hex_to_rgb('#D939CD')],

    [hex_to_rgb('#CE9FFC'), hex_to_rgb('#7367F0')],
    [hex_to_rgb('#90F7EC'), hex_to_rgb('#32CCBC')],
    [hex_to_rgb('#FFF6B7'), hex_to_rgb('#F6416C')],

    [hex_to_rgb('#92FFC0'), hex_to_rgb('#002661')],
    [hex_to_rgb('#EEAD92'), hex_to_rgb('#6018DC')],
    [hex_to_rgb('#F6CEEC'), hex_to_rgb('#D939CD')],
]

# 随机选择一个渐变色彩范围
color_range = random.choice(color_ranges)

# 设置图片宽高
img_width = 800
img_height = 320

#设置图片圆角值
roundness = 5

# 创建一个大小为img_width x img_height的图片，渐变色
img = Image.new('RGB', (img_width, img_height), color=(255, 0, 0))
draw = ImageDraw.Draw(img)
width, height = img.size
for x in range(width):
    for y in range(height):
        r = int(color_range[0][0] + (color_range[1][0] - color_range[0][0]) * x / width)
        g = int(color_range[0][1] + (color_range[1][1] - color_range[0][1]) * y / height)
        b = int(color_range[0][2] + (color_range[1][2] - color_range[0][2]) * (x + y) / (width + height))
        draw.point((x, y), (r, g, b))

# 设置自定义标题
title = "我是自定义标题"
font = ImageFont.truetype("msyh.ttc", 36, encoding='utf-8')
title_bbox = draw.textbbox((0, 0), title, font=font)
left = (img_width - title_bbox[2]) // 2
top = (img_height - title_bbox[3]) // 2
lines = []
line = ""
for word in title.split():
    if len(line + word) > 20:
        lines.append(line)
        line = ""
    line += word + " "
lines.append(line)
for i, line in enumerate(lines):
    title_bbox = draw.textbbox((0, 0), line, font=font)
    left = (img_width - title_bbox[2]) // 2
    top = (img_height - title_bbox[3]*len(lines)) // 2
    draw.text((left, top + i*40), line, font=font, fill=(255, 255, 255))

# 加载指定图片按比例缩放后显示在创建的图片中间，上下左右最小保留40px间距

#im = Image.open('avatar.jpg')
#im.thumbnail((img_width-20, img_height-20))
#left = (img_width - im.width) // 2
#top = (img_height - im.height) // 2
#img.paste(im, (left, top))

# 将图片改为圆角
mask = Image.new('L', (img_width, img_height), 0)
draw = ImageDraw.Draw(mask)

draw.rounded_rectangle((0, 0, img_width, img_height), roundness, fill=255)
img.putalpha(mask)

# 保存图片
img.save('output.png')

